import { Injectable } from '@angular/core'
import { HttpClientService } from 'src/app/common/httpClient/http'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage'

@Injectable({
  providedIn: 'root'
})
export class SearchResultService {
  start = ''
  end = ''
  zone = ''
  keyword = ''
  reqdata = {
    pager: { Start: 0, Limit: 10 },
    filter: {
      Lambda: '',
      Navigations: []
    },
    sorters: [
      { Property: 'item=>item.Sort', Direction: 'Descending' },
      { Property: 'item=>item.Id', Direction: 'Descending' }
    ]
  }

  constructor(private http: HttpClientService, private utils: CommonUtil, private localStorage: CoolLocalStorage) {
    this.start = this.utils.time2UTC(new Date())
    this.end = this.utils.time2UTC(new Date())
    this.zone = this.localStorage.getItem('zone')
  }

  getList(Start, callback, errorBack?) {
    let lambda = `item=>item.Publish&&item.Start<=${this.start}&&item.End>=${this.end}&&item.Zone==${
      this.zone
    }&&(item.ProjectInfo.Name.Contains("${this.keyword}")||item.ProjectInfo.Alias.Contains("${
      this.keyword
    }")|| item.ProjectInfo.Address.Contains("${this.keyword}"))`
    this.reqdata.pager.Start = Start
    this.reqdata.filter.Lambda = lambda
    this.http.post(this.reqdata, '/House1/Json/CooperationService.svc/QueryProjectSummary', callback, errorBack)
  }
}
